import json
from email import encoders
from email.mime.base import MIMEBase

import requests as http
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
from email.mime.multipart import MIMEMultipart
from email.message import EmailMessage
from email.mime.application import MIMEApplication

url = "http://192.168.5.158/Order/GetJDOrderList"

d = open('./time.txt').read()
# print(d)
headers = {
    "Content-Type": "application/json"
}

testData = """
{
    "data": [
        {
            "outOrderNumber": "1639540612003133",
            "jdOrderId": null,
            "shippingAddress": "广东省广州市天河区华景新城",
            "mobile": "15018355155",
            "consignee": "测试",
            "batchUrl": "https://item.jd.com/903427.html"
        },
        {
            "outOrderNumber": "1639546849003134",
            "jdOrderId": null,
            "shippingAddress": "上海市上海市浦东新区高桥镇人民政府",
            "mobile": "15018355155",
            "consignee": "测试",
            "batchUrl": "https://item.jd.com/903427.html"
        },
        {
            "outOrderNumber": "1639547055003135",
            "jdOrderId": null,
            "shippingAddress": "上海市上海市浦东新区上海市上海市浦东新区高桥镇人民政府",
            "mobile": "15018355155",
            "consignee": "测试",
            "batchUrl": "https://item.jd.com/903427.html"
        }
    ],
    "code": 0,
    "message": null
}
"""

def getData():
    return json.loads(testData)
    # req = http.post(url=url, json=json.loads(d))
    # print(f'req -->> {req}')
    # if req.status_code != 200:
    #     print('请求失败')
    #     return None
    # print('请求成功')
    # print(req.text)
    # return json.loads(req.text)


def sendEmail(centent):
    my_sender = 'a15018355155@163.com'
    my_pass = 'OVXSBDSOVWGIKAXP'
    my_user = '17685034710@163.com'
    ret = True
    try:

        f = open('./report.html', 'rb')
        mail_body = f.read()
        f.close()
        body = MIMEText(mail_body, 'html ', 'utf-8')  # 填写邮件内容

        msg = MIMEMultipart()
        msg['From'] = formataddr(["tracy", my_sender])  # 括号里的对应发件人邮箱昵称、发件人邮箱账号
        msg['To'] = formataddr(["test", my_user])  # 括号里的对应收件人邮箱昵称、收件人邮箱账号
        msg['Subject'] = "发送邮件测试"  # 邮件的主题，也可以说是标题
        msg.attach(body)
        # 附件添加

        # att = MIMEText(mail_body, "base64", "utf-8")
        # att["Content-Type"] = "application/octet-stream"

        # att["Content-Disposition"] = 'attachment;filename="report.html"'
        # msg.attach(att)

        server = smtplib.SMTP_SSL("smtp.163.com", 465)  # 发件人邮箱中的SMTP服务器
        server.login(my_sender, my_pass)  # 括号中对应的是发件人邮箱账号、邮箱授权码
        server.sendmail(my_sender, [my_user, ], msg.as_string())  # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
        server.quit()  # 关闭连接
    except Exception:  # 如果 try 中的语句没有执行，则会执行下面的 ret=False
        ret = False
    return ret


def getPath(p):
    res = http.get("https://apis.map.qq.com/ws/geocoder/v1/", {
        "key": "WMNBZ-S7TCU-ZSNVV-2OL56-HWHXT-IUFSA",
        "smart_address": p
    })
    res.encoding = "utf-8"
    return res.json()

def pushOrderNumber(num):
    api = 'http://192.168.5.158/Order/UpdateSupplierOrderNumber'
    # 使用post提交订单号，数据是json格式，如果不行可以换成data格式
    http.post(url=api, data={
        "outOrderNumber": num,
        "supplierOrderNumber": ""
    })


class Config:
    def __init__(self):
        self.index = 0
        self.indexApi = ''
        self.loginApi = 'https://passport.jd.com/new/login.aspx'
        self.testData = getData()
        self.user = '17685034710'
        self.pasw = '340822chen'
